package com.kabongos.activities;

import java.io.ByteArrayOutputStream;
import java.sql.Date;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageButton;

import com.kabongos.facade.Facade;
import com.kabongos.sources.Clube;

public class EditaClubeActivity extends Activity {
    private static final int SELECT_IMAGE = 100;
    private String imagePath;
    // private Bitmap escudo;
    private byte[] escudo;
    EditText etNomeClube;
    DatePicker dpDataFundacaoClube;
    Button btConfirmarClube;
    ImageButton ibtSelecImg;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        Log.i("DEBUG", "Entrei no edita CLUBE!");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.cadastro_clube);
        Log.i("DEBUG", "dentro do edita");

        Bundle extras = this.getIntent().getExtras();
        final Clube clube = (Clube) extras.getSerializable("clube");
        etNomeClube = (EditText) findViewById(R.id.etNomeClube);
        dpDataFundacaoClube = (DatePicker) findViewById(R.id.dpDataFundacaoClube);

        etNomeClube.setText(clube.getNome());

        if (clube.getEscudo() != null)
            escudo = clube.getEscudo();

        btConfirmarClube = (Button) findViewById(R.id.btConfirmarClube);
        btConfirmarClube.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                obtemDados(clube.getMatricula());
                Intent i = new Intent(getApplicationContext(), MainActivity.class);
                startActivity(i);

            }
        });
        ibtSelecImg = (ImageButton) findViewById(R.id.ibtSelecImg);
        if (clube.getEscudo() != null) {
            ibtSelecImg.setImageBitmap(Bitmap.createScaledBitmap(
                    BitmapFactory.decodeByteArray(clube.getEscudo(), 0, clube.getEscudo().length), 150, 150, false));
        }
        ibtSelecImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_IMAGE);

            }
        });

    }

    private void obtemDados(long matricula) {
        Clube clube = new Clube(etNomeClube.getText().toString(), new Date(dpDataFundacaoClube.getYear() - 1900,
                dpDataFundacaoClube.getMonth(), dpDataFundacaoClube.getDayOfMonth()));
        clube.setMatricula(matricula);
        clube.setEscudo(escudo);
        Log.i("DEBUG", "Nome Clube " + clube.getNome());
        Log.i("DEBUG", "Data Clube " + clube.getDataFundacao().toString());
        Facade facade = new Facade();
        facade.atualizaClube(clube);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
        super.onActivityResult(requestCode, resultCode, imageReturnedIntent);

        switch (requestCode) {
        case SELECT_IMAGE:
            if (resultCode == RESULT_OK) {
                Uri selectedImage = imageReturnedIntent.getData();
                String[] filePathColumn = { MediaStore.Images.Media.DATA };
                Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
                cursor.moveToFirst();
                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                imagePath = cursor.getString(columnIndex);
                cursor.close();
                Bitmap imgEscudo;
                imgEscudo = BitmapFactory.decodeFile(imagePath);
                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                imgEscudo.compress(Bitmap.CompressFormat.PNG, 100, stream);
                escudo = stream.toByteArray();
                ibtSelecImg.setImageBitmap(Bitmap.createScaledBitmap(imgEscudo, 150, 150, false));
            }
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_cadastro_clube, menu); // tem
                                                                         // que
                                                                         // ver
                                                                         // isso
        return true;
    }

}
